package com.example.demo.services;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.utils.HttpSends;

/**
 * @author Buer
 * @update 2021/4/27/02713:46
 */
@Component
public class WeChatSubscribeMessage {
    /** 全局配置appid */
    @Value("${app.openid}")
    private String appid;
    /** 全局配置secret */
    @Value("${app.secret}")
    private String secret;
    /** 全局配置templateID */
    @Value("${app.template}")
    private String template;
    /** 全局配置获取access_token的url */
    @Value("${app.url2}")
    private String url2;
    /** 全局配置发送模板消息的url */
    @Value("${app.url3}")
    private String url3;

    /** 获取access_token */
    public void getAccessToken() {
        HttpSends httpSends = new HttpSends();
        // 转换登录请求网址
        String url = url2.replace("APPID", appid).replace("APPSECRET", secret);
        // 请求信息,返回字符串
        String result = httpSends.sendGet(url);
        // 字符串转JSON格式
        JSONObject jsonobject = JSONObject.parseObject(result);
        sendSubscribeMessage(jsonobject.getString("access_token"));
    }

    /** 使用access_token发送订阅消息 */
    public void sendSubscribeMessage(String token) {
        System.out.println("获取到 token->" + token);
        HttpSends httpSends = new HttpSends();
        // 字符串格式模板数据
        String str =
            "{\n" + "  \"touser\": \"OPENID\",\n" + "  \"template_id\": \"TEMPLATE\",\n" + "  \"page\": \"index\",\n"
                + "  \"miniprogram_state\": \"developer\",\n" + "  \"lang\": \"zh_CN\",\n" + "  \"data\": {\n"
                + "    \"thing4\": {\n" + "      \"value\": \"TITLE\"\n" + "    },\n" + "    \"thing2\": {\n"
                + "      \"value\": \"NAME\"\n" + "    },\n" + "    \"thing6\": {\n" + "      \"value\": \"SITE\"\n"
                + "    },\n" + "    \"date3\": {\n" + "      \"value\": \"TIME\"\n" + "    },\n" + "    \"thing7\": {\n"
                + "      \"value\": \"活动将于三十分钟后开始！请按时参加！\"\n" + "    }\n" + "  }\n" + "}";
        // 替换用户(openid),模板id,卖家昵称，截止日期，商品名称
        String param = str.replace("OPENID", "obnVb5MyM-kq0X3RJG5Jyq9S7wj8").replace("TEMPLATE", template)
            .replace("TITLE", "‘微心愿’志愿者活动").replace("NAME", "服务人民，服务生活。").replace("SITE", "第六教学楼110")
            .replace("TIME", "2021-04-27 20:50:55");
        // 转换登录请求网址
        String url = url3.replace("ACCESS_TOKEN", token);
        String result = httpSends.sendPost(url, param);
        // 判断订阅消息是否发送成功
        boolean isSuccess = result.contains("ok");
        // 答应返回数据
        if (isSuccess) {
            System.out.println("模板消息发送成功->" + result);
        } else {
            System.out.println("模板消息发送失败->" + result);
        }
    }
}
